package io.github.talelin.sleeve.controller.v1;

import io.github.talelin.sleeve.module.message.WsHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@RestController
@RequestMapping("/wsc")
public class WscTestController {
    @Autowired
    private WsHandler wsHandler;

    @RequestMapping("/broadCast")
    public String index() {
        try {
            wsHandler.broadCast("广播一条消息");
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 省略其它代码
        return "广播";
    }
    @RequestMapping("/toOne")
    public String index2() {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        String user = request.getHeader("user");
        try {
            wsHandler.sendMessage(Integer.valueOf(user),"通过UserId广播一条消息");
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 省略其它代码
        return "广播";
    }
}
